package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.rJson;
import cn.acewill.yxd.yunxundian.model.sType;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;

/**
 * Created by XXX on 2017/6/1.
 */

public class TaskQuestionAdapter extends BaseAdapter{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<tempLostRR> list;
    private  ArrayList<RMain> rMains;

    public TaskQuestionAdapter(Context context, List<tempLostRR> list){
        this.context=context;
        this.list=list;
        try {
            loadtitle();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.task_question_info,parent,false);
            holder=new ViewHolder();
            holder.TaskTitle= (TextView) convertView.findViewById(R.id.TaskTitle);
            holder.taskDesc= (TextView) convertView.findViewById(R.id.taskDesc);
            holder.mycheck= (CheckBox) convertView.findViewById(R.id.mycheck);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        //此处设置对象的数据
        tempLostRR bean=list.get(position);
        if (TextUtils.isEmpty(bean.getDescStr())){
            holder.taskDesc.setVisibility(View.GONE);
        }else{
            holder.taskDesc.setVisibility(View.VISIBLE);
            holder.taskDesc.setText(bean.getDescStr());
        }
        //查询标题的名称
        String name =loaddata(rMains,list.get(position).getRRowId());
        holder.TaskTitle.setText(name);
        holder.mycheck.setChecked(bean.ischeck());
        return convertView;
    }

    class ViewHolder{
        TextView TaskTitle;
        TextView taskDesc;
        CheckBox mycheck;
    }

    public void loadtitle() throws DbException {
        Schedule schedule=db.selector(Schedule.class).where("ID","=",list.get(0).getSchId()).findFirst();
        sType type=db.selector(sType.class).where("ID","=",schedule.getSTypeID()).findFirst();
        rJson rmian=db.selector(rJson.class).where("ID","=",type.getReportId()).findFirst();
        String json=rmian.getjSon();
        Gson gson=new Gson();
        Type typeList = new TypeToken<ArrayList<RMain>>() {
        }.getType();
        rMains= gson.fromJson(pubClass.convertToArrayJsonNormal(json), typeList);
    }

    public static String loaddata(ArrayList<RMain> rMains,int rowid){

        for (int i=0;i<rMains.size();i++){
            List<RMain.TablesBean> tb = rMains.get(i).getTables();
            for (RMain.TablesBean t : tb){
                List<RMain.TablesBean.RowsBean> rbs = t.getRows();
                for (RMain.TablesBean.RowsBean rb : rbs){
                    if (rb.getQ2().getID() == rowid) {
                        return rb.getQ2().getSName()+" "+rb.getQ2().getReportRowName();
                    }
                }
            }
        }
        return null;
    }
}
